package fr.nghs.android.thport.gestures;

import fr.nghs.android.common.MathUtils;
import fr.nghs.android.common.ScaleGestureDetector;
import fr.nghs.android.thport.THSurface;

/**
 * Surface offset only.
 * Zoom is performed from an alternative "zoom bar"
 * @author ng
 *
 */
public class SurfaceMoveOnly
    implements ScaleGestureDetector.OnScaleGestureListener
    //implentes ZoomBar.OnZoomChangedListener //TODO
{
    private float prevX, prevY, zoom = 1f;
    private int xOffset = 0, yOffset = 0, w, h;

    private THSurface surface;
    
    public SurfaceMoveOnly(THSurface s)
    {
        surface = s;
    }
    
    @Override
    public boolean onScaleBegin(ScaleGestureDetector s)
    {
        prevX = Float.NaN;
        w = surface.getWidth();
        h = surface.getHeight();
        return true;
    }
    
    @Override
    public boolean onScale(ScaleGestureDetector s)
    {
        final float x = s.getFocusX();
        final float y = s.getFocusY();

        boolean isFirst = Float.isNaN(prevX);
        
        if(isFirst)
        {
        }
        else
        {
            xOffset += x - prevX;
            yOffset += y - prevY;
            
            yOffset = MathUtils.ensureRange(yOffset, (int)(h * (1 - zoom)), 0);
            xOffset = MathUtils.ensureRange(xOffset, (int)(w * (1 - zoom)), 0);

            surface.setOffsetAndScale(xOffset, yOffset, zoom);
        }
        
        prevX = x;
        prevY = y;
        return true;

    }

    @Override
    public void onScaleEnd(ScaleGestureDetector s)
    {

    }

}
